﻿/*
* This file is part of WinMergeFS.
*
* WinMergeFS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.

* WinMergeFS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WinMergeFS.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using WinMergeFS.Common;
using RealtimeTrace;

namespace WinMergeFS.API
{
    class RootSelectInbuildPlugin : IRootSelectPlugin
    {
        private Profile _profile;
        private LoggerEventHandler _logger;
        private RootSelectHelper _rootselecthelper;

        #region IRootSelectPlugin Member

        string IRootSelectPlugin.PluginAuthor
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string IRootSelectPlugin.PluginDescription
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string IRootSelectPlugin.PluginID
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        string IRootSelectPlugin.PluginVersion
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        void IRootSelectPlugin.PluginRegister(Profile profile, LoggerEventHandler logger, ref Dictionary<string, RootSelectPluginDelegate> fileratinghandler, ref Dictionary<string, RootSelectPluginDelegate> dirratinghandler)
        {
            _profile = profile;
            _logger = logger;
            _rootselecthelper = new RootSelectHelper(_profile);
            fileratinghandler.Add("force", new RootSelectPluginDelegate(Force));
            dirratinghandler.Add("force", new RootSelectPluginDelegate(Force));
            fileratinghandler.Add("diskspace", new RootSelectPluginDelegate(Diskspace));
            dirratinghandler.Add("diskspace", new RootSelectPluginDelegate(Diskspace));
        }

        #endregion

        public void Force(Profile profile, ref List<int> rootrating, string filename)
        {
            try
            {
                rootrating[(int)profile.Plugins["force"].Parameter] = -2;
                _logger("Plugin_force: " + profile.Plugins["force"].Parameter.ToString(), LogLevel.debug);
            }
            catch (Exception ex)
            { TraceFactory.InsertTrace(this, ex, profile, rootrating, filename); }
        }

        public void Diskspace(Profile profile, ref List<int> rootrating, string filename)
        {
            try
            {
                List<ulong> freespace = new List<ulong>();

                for (int i = 0; i < profile.Roots.Count; i++)
                {
                    freespace.Add(_rootselecthelper.GetSpaceInfo(i).FreeSpaceAvailable);
                }

                ulong max = freespace.Max();

                for (int i = 0; i < profile.Roots.Count; i++)
                {
                    if (rootrating[i] >= 0) rootrating[i] += (int)(profile.Plugins["diskspace"].Multiply * (1.0 * freespace[i]) / max);
                    _logger("Plugin_diskspace: (" + i + ") Free: " + freespace[i] + ", Rating: " + rootrating[i], LogLevel.debug);
                }

            }
            catch (Exception ex)
            { TraceFactory.InsertTrace(this, ex, profile, rootrating, filename); }

        }

    }
}
